//不允许用多余的空间，只能在原数组上修改，由于最多只会出现两次，则将下标为nums[i]-1的位置上置为负
import java.util.ArrayList;
import java.util.List;

public class Solution442 {
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> ans=new ArrayList<>();
        for (int i=0;i<nums.length;i++){
            if (nums[Math.abs(nums[i])-1]<0){
                ans.add(Math.abs(nums[i]));
            }else{
                nums[Math.abs(nums[i])-1]=-nums[Math.abs(nums[i])-1];
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution442().findDuplicates(new int[]{4,3,2,7,8,2,3,1}));
    }
}
